package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.storage.domain.SubcontractReceipt;
import com.pureut.storage.domain.SubcontractReceiptDetail;
import com.pureut.storage.dto.SubcontractReceiptDetailDto;
import com.pureut.storage.mapper.SubcontractReceiptDetailMapper;
import com.pureut.storage.mapper.SubcontractReceiptMapper;
import com.pureut.storage.service.SubcontractReceiptDetailService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.dict.utils.DictUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.model.MaterialUnitDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * 委外入库明细实现层
 * Author:C
 * Date:2023/01/18 16:59
 * @DESC
 */
@Service
public class SubcontractReceiptDetailServiceImpl  implements SubcontractReceiptDetailService {

    @Resource
    SubcontractReceiptDetailMapper subcontractReceiptDetailMapper;

    @Resource
    SubcontractReceiptMapper subcontractReceiptMapper;

    @DubboReference
    RemoteUnitService remoteUnitService;

    /**
     * 委外入库明细列表
     *
     * @param id
     * @return
     */
    @Override
    public TableDataInfo<SubcontractReceiptDetailDto> getSubcontractReceiptDetail(Long id, PageQuery pageQuery) {

//        //获取网型标准缓存字典数据
//        List<SysDictData> netArray = DictUtils.getDictCache("sys_of_type");
//        Map<String, String> netMap = netArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
//        //获取铺网类型标准缓存字典数据
//        List<SysDictData> netTypeArray = DictUtils.getDictCache("sys_type_net");
//        Map<String, String> netTypeMap = netTypeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        Page<SubcontractReceiptDetailDto> subcontractReceiptDetailList = subcontractReceiptDetailMapper.getSubcontractReceiptDetailList(id,pageQuery.build());

        SubcontractReceipt subcontractReceipt = subcontractReceiptMapper.selectOne(new QueryWrapper<SubcontractReceipt>()
            .lambda().eq(SubcontractReceipt::getId, id));

        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();
        //获取委外入库明细状态
        List<SysDictData> detailStatusArray = DictUtils.getDictCache("subcontract_receipt_detail_status");
        Map<String, String> detailStatusMap = detailStatusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        List<SubcontractReceiptDetailDto> records = subcontractReceiptDetailList.getRecords();

        for (SubcontractReceiptDetailDto entity : records) {
            //状态
            entity.setStatusDict(detailStatusMap.get(entity.getStatus()));
            //物料单位
            if (entity.getMaterialUnit() != null) {
                entity.setUnitDict(unitByCodeToMap.get(entity.getMaterialUnit()).getUnitName());
            }
            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + subcontractReceipt.getTenantId(), String.valueOf(entity.getMaterialId()));
            entity.setAuxiliaryAttribute(statsArray);
        }
        return TableDataInfo.build(subcontractReceiptDetailList);
    }

    /**
     * 批量保存
     * @param list
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSaveList(List<SubcontractReceiptDetail> list) {
        return subcontractReceiptDetailMapper.insertBatch(list);
    }
}
